package com.tupu.controller.permission;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tupu.common.Result;
import com.tupu.entity.permission.RoleInfo;
import com.tupu.service.permission.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired(required = false)
    private RoleService roleService;

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PostMapping("/add")
    public Result addRole(@RequestBody(required = false) RoleInfo role) {
        try {
            Integer result = roleService.addRole(role);
            if (result == 1) {
                return Result.ok("添加成功");
            } else {
                //if (result == 0)
                return Result.errorMessage("角色已存在");
            }
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    /**
     * 通过role_id 删除角色(可批量删除)
     *
     * @param role_id
     * @return
     */
    @PostMapping("/del")
    public Result delRole(@RequestParam(required = false) List<Integer> role_id) {
        try {
            roleService.delRole(role_id);
            return Result.ok("删除成功");
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    /**
     * 通过role_id 查找当前角色的所拥有权限
     *
     * @param role_id
     * @return
     */
    @PostMapping("/findOne")
    public Result<RoleInfo> findOne(@RequestParam(required = false) Integer role_id) {
        try {
            RoleInfo role = roleService.findById(role_id);
            return Result.ok(role);
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    /**
     * 更新角色
     *
     * @param role
     * @return
     */
    @PostMapping("/update")
    public Result updateRole(@RequestBody(required = false) RoleInfo role) {
        try {
            Integer result = roleService.updateRole(role);
            if (result == 1) {
                return Result.ok("修改成功");
            } else {
                //if (result == 0)
                return Result.errorMessage("角色已存在");
            }
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    /**
     * 角色列表 分页 模糊搜索
     *
     * @param page
     * @param size
     * @param name
     * @return
     */
    @PostMapping("/list")
    public Result<Page<RoleInfo>> PageInfo(@RequestParam(required = false, defaultValue = "1") Integer page,
                                           @RequestParam(required = false, defaultValue = "10") Integer size,
                                           @RequestParam(required = false) String name) {
        try {
            Page<RoleInfo> pageInfo = roleService.PageInfo(page, size, name);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    /**
     * 角色列表 不分页 全部
     *
     * @return
     */
    @PostMapping("/listAll")
    public Result<List<RoleInfo>> findAll() {
        List<RoleInfo> roleInfo = roleService.findAll();
        return Result.ok(roleInfo);
    }

}